import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:universal_platform/universal_platform.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../utils/CommUtils.dart';
import '../view/_OverScrollBehavior.dart';
import '../wight/My.dart';

/**
 * Web文档
 */

class WebPage extends StatefulWidget {
  String titel; //标题
  String url;
  WebPage({Key? key, required this.titel, required this.url}) : super(key: key);
  @override
  State<WebPage> createState() => _State();
}

final Completer<WebViewController> _controller = Completer<WebViewController>();

class _State extends State<WebPage> {
  @override
  void initState() {
    print("url是:"+widget.url.toString());
    if (UniversalPlatform.isAndroid) {
      WebView.platform = SurfaceAndroidWebView();
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Image.asset('assets/images/top_leaves.png',
              width: double.infinity,
              height: ScreenUtil().setHeight(30),
              fit: BoxFit.fill),
          SizedBox(
            height: ScreenUtil().setHeight(2),
          ),
          //顶部垂直居中
          Container(
            padding: EdgeInsets.only(left: 8),
            height: ScreenUtil().setHeight(35),
            child: Stack(
              children: [
                Align(
                  alignment: Alignment.centerLeft,
                  child: GestureDetector(
                    onTap: (() {
                      Navigator.pop(context);
                    }),
                    child: Image.asset('assets/images/back.png',
                         width: ScreenUtil().setWidth(40),
                          height: ScreenUtil().setHeight(35),
                        fit: BoxFit.contain),
                  ),
                ),
                Align(
                  alignment: Alignment.center,
                  child: CommUtils.getTextGame(
                      context, 20, widget.titel, textColor, textbgColor),
                ),
              ],
            ),
          ),

          // _flag ? _getMoreWidget() : Text(""),
          Expanded(
              child: ScrollConfiguration(
            behavior: OverScrollBehavior(),
            child: Padding(
              padding: EdgeInsets.only(
                left: ScreenUtil().setWidth(5),
                right: ScreenUtil().setWidth(5),
              ),
              child: Stack(
                children: [
                  WebView(
                    initialUrl: widget.url,
                    javascriptMode: JavascriptMode.unrestricted,
                    //   onWebViewCreated: (WebViewController webViewController) {
                    //      _controller.complete(webViewController);
                    // },
                    onProgress: (int progress) {
                      print('WebView is loading (progress : $progress%)');
                    },
                    javascriptChannels: <JavascriptChannel>{
                      _toasterJavascriptChannel(context),
                    },
                    // navigationDelegate: (NavigationRequest request) {
                    //   if (request.url.startsWith('https://www.baidu.com/')) {
                    //     print('blocking navigation to $request}');
                    //     return NavigationDecision.prevent;
                    //   }
                    //   print('allowing navigation to $request');
                    //    return NavigationDecision.navigate;
                    // },
                    onPageStarted: (String url) {
                      print('Page111 started loading: $url');
                    },
                    onPageFinished: (String url) {
                      setState(() {
                         isFilst=true;
                      });
                      print('Page1111 finished loading: ');
                    },
                    // gestureNavigationEnabled: true,
                    // backgroundColor: const Color(0x00000000),
                  ),
                  _getMoreWidget()
                ],
              ),
            ),
          )),

          // Image.asset('assets/images/botton_.png',
          //     width: double.infinity,
          //     height: ScreenUtil().setHeight(30),
          //     fit: BoxFit.fill),
        ],
      ),
    );
  }

  JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
    return JavascriptChannel(
        name: 'Toaster',
        onMessageReceived: (JavascriptMessage message) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(message.message)),
          );
        });
  }

  bool isFilst = false;

  Widget _getMoreWidget() {
    return isFilst
        ? Text("")
        : Align(
            alignment: Alignment.center,
            child: CircularProgressIndicator(
              strokeWidth: 1.0,
            ),
          );
  }
}
